package com.hjm.java.service;


import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.hjm.java.entity.UserInfo;
import com.hjm.java.entity.UserLogin;
import com.hjm.java.entity.UserRegister;
import com.hjm.java.sql.HJMHelper;
import com.hjm.java.utils.HjmFailCallBack;
import com.hjm.java.utils.HjmGlideUtil;
import com.hjm.java.utils.HjmObjectCallBack;
import com.hjm.java.utils.HjmSuccessCallBack;
import com.hjm.java.utils.HttpUtils;

/**
 * 用户信息服务
 */
public class UserService extends BaseService {

    public UserService(Context mContext, Handler mHandler) {
        super(mContext, mHandler);
    }

    public void getUserInfo(HjmObjectCallBack<UserInfo> callBack) {
        httpUtils.getByToken(baseUrl + "/user/info", UserInfo.class, (isSuccess, object, msg) -> {
            if (isSuccess) {
                UserInfo userInfo = object;
                HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl, userInfo.getProfilePhoto().toString(),
                        HJMHelper.getInstance(mContext),
                        (success, bitmap, message) -> {
                            mHandler.post(() -> {
                                if (success) {
                                    userInfo.setProfilePhotoBitmap(bitmap);
                                    callBack.call(true, userInfo, null);
                                } else {
                                    callBack.call(false, null, message);
                                }
                            });
                        });
            } else {
                mHandler.post(() -> {
                    callBack.call(false, null, msg);
                });
            }
        });
    }

    public void login(UserLogin userLogin, HjmSuccessCallBack successCallBack, HjmFailCallBack failCallBack) {
        httpUtils.post(baseUrl + "/user/login", userLogin, String.class, (isSuccess, object, msg) -> {
            mHandler.post(() -> {
                if (isSuccess) {
                    // 保存token到SharedPreferences
                    HttpUtils.saveTokenToSharedPreferences(mContext, "token", object);
                    successCallBack.onSuccess();
                } else {
                    failCallBack.onFail(msg);
                }
            });
        });
    }

    public void register(UserRegister userRegister, HjmSuccessCallBack successCallBack, HjmFailCallBack failCallBack) {
        httpUtils.post(baseUrl + "/user/register", userRegister, String.class, (isSuccess, string, msg) -> {
            mHandler.post(() -> {
                if (isSuccess) {
                    successCallBack.onSuccess();
                } else {
                    failCallBack.onFail(msg);
                }
            });
        });
    }

    public void updateUserInfo(UserInfo userInfo, HjmSuccessCallBack successCallBack, HjmFailCallBack failCallBack) {
        UserInfoTmp userInfoTmp = new UserInfoTmp();
        userInfoTmp.setNickName(userInfo.getNickName());
        userInfoTmp.setPasswordMd5(userInfo.getPasswordMd5());
        httpUtils.putByToken(baseUrl + "/user/info", userInfoTmp, String.class, (isSuccess, string, msg) -> {
            mHandler.post(() -> {
                if (isSuccess) {
                    successCallBack.onSuccess();
                } else {
                    failCallBack.onFail(msg);
                }
            });
        });
    }

    public void logout(HjmSuccessCallBack successCallBack, HjmFailCallBack failCallBack) {
        httpUtils.postByToken(baseUrl + "/user/logout", null, String.class, (HjmObjectCallBack<String>) (isSuccess, object, msg) -> {
            mHandler.post(() -> {
                if (isSuccess) {
                    HttpUtils.deleteTokenFromSharedPreferences(mContext, "token");
                    successCallBack.onSuccess();
                } else {
                    failCallBack.onFail(msg);
                }
            });
        });
    }

    class UserInfoTmp{
        private String nickName;
        private String passwordMd5;

        public String getNickName() {
            return nickName;
        }

        public void setNickName(String nickName) {
            this.nickName = nickName;
        }

        public String getPasswordMd5() {
            return passwordMd5;
        }

        public void setPasswordMd5(String passwordMd5) {
            this.passwordMd5 = passwordMd5;
        }
    }
}
